package com.houhanzhi.service.impl;

import com.houhanzhi.controller.request.LinkGroupAddRequest;
import com.houhanzhi.controller.request.LinkGroupUpdateRequest;
import com.houhanzhi.interceptor.LoginInterceptor;
import com.houhanzhi.manager.LinkGroupManager;
import com.houhanzhi.model.LinkGroupDO;
import com.houhanzhi.service.LinkGroupService;
import com.houhanzhi.vo.LinkGroupVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author houhanzhi
 * @since 2025-02-26
 */
@Service
public class LinkGroupServiceImpl implements LinkGroupService {

    @Autowired
    private LinkGroupManager linkGroupManager;

    /**
     * 新增分组
     * @param request
     * @return
     */
    @Override
    public int add(LinkGroupAddRequest request) {
        // 防止水平越权
        final long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        LinkGroupDO linkGroupDO = new LinkGroupDO();
        linkGroupDO.setTitle(request.getTitle());
        linkGroupDO.setAccountNo(accountNo);
        int rows = linkGroupManager.add(linkGroupDO);
        return rows;
    }

    /**
     * 根据id删除分组
     * @param groupId
     * @return
     */
    @Override
    public int del(Long groupId) {
        // 防止水平越权
        final long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();

        return linkGroupManager.del(groupId, accountNo);
    }

    /**
     * 根据id查询详情
     * @param groupId
     * @return
     */
    @Override
    public LinkGroupVO detail(Long groupId) {
        // 防止水平越权
        final long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        LinkGroupDO linkGroupDO = linkGroupManager.detail(groupId, accountNo);
        final LinkGroupVO linkGroupVO = new LinkGroupVO();

        // 或者也可以用map-struct
        BeanUtils.copyProperties(linkGroupDO, linkGroupVO);
        return linkGroupVO;
    }

    /**
     * 查询用户全部分组
     * @return
     */
    @Override
    public List<LinkGroupVO> listAllLinkGroup() {
        // 防止水平越权
        final long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        List<LinkGroupDO> linkGroupDOList = linkGroupManager.listAllLinkGroup(accountNo);
        final List<LinkGroupVO> linkGroupVOList = linkGroupDOList.stream().map(obj -> {
            final LinkGroupVO linkGroupVO = new LinkGroupVO();
            BeanUtils.copyProperties(obj, linkGroupVO);
            return linkGroupVO;
        }).collect(Collectors.toList());
        // 或者也可以用map-struct
        return linkGroupVOList;
    }

    /**
     * 更新组名
     * @param linkGroupUpdateRequest
     * @return
     */
    @Override
    public int update(LinkGroupUpdateRequest linkGroupUpdateRequest) {
        // 防止水平越权
        final long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        final LinkGroupDO linkGroupDO = new LinkGroupDO();
        linkGroupDO.setTitle(linkGroupUpdateRequest.getTitle());
        linkGroupDO.setId(linkGroupUpdateRequest.getId());
        linkGroupDO.setAccountNo(accountNo);
        int rows = linkGroupManager.update(linkGroupDO);
        return rows;
    }
}
